﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MSBTC.Model;

namespace MSBTC.WinForms
{

    public partial class DeliveryListForm : Form
    {
        private string Datamanip;
        private int rowselect;
        MDIParent mdipar = new MDIParent();
        PMSContext dbcontext = new PMSContext();
        Customer dbcus = new Customer();

        public DeliveryListForm()
        {
            InitializeComponent();
        }

        private void DeliveryListForm_Load(object sender, EventArgs e)
        {
            RefreshDB();
        }
        
        #region functions
        private void RefreshDB()
        {
            var del = (from c in dbcontext.DeliveryReceipts
                       select c).ToList();

            LVDelivListInfo.Items.Clear();
            if (del != null)
            {

                foreach (var sqlDel in del)
                {
                    ListViewItem lvitems = LVDelivListInfo.Items.Add(sqlDel.DRid_PK.ToString());
                    lvitems.SubItems.Add(sqlDel.InvoiceNo.ToString());
                    lvitems.SubItems.Add(sqlDel.Receivedby.ToString());
                    lvitems.SubItems.Add(sqlDel.DateTransact.ToString());

                    var po = dbcontext.PurchOrder.Find(sqlDel.poid_FK);
                    lvitems.SubItems.Add(po.POnum.ToString());
                }
            }
            this.Refresh();
        }



        private void DisableFields()
        {
            //txtCname.Enabled = false;
            //txtAddr.Enabled = false;
            //txtCity.Enabled = false;
            //txtDesc.Enabled = false;
            //txtEadd.Enabled = false;
            //txtCnum.Enabled = false;

        }

        private void EnableFields()
        {
            //txtCname.Enabled = true;
            //txtAddr.Enabled = true;
            //txtCity.Enabled = true;
            //txtDesc.Enabled = true;
            //txtEadd.Enabled = true;
            //txtCnum.Enabled = true;

        }

        private void RefreshFields()
        {
            DisableFields();
            RefreshDB();

            //btnCNew.Enabled = true;
            //btnCDel.Enabled = false;
            //btnCEdit.Enabled = false;
            //btnClear.Enabled = true;
            //btnCSave.Enabled = false;
        }
        #endregion

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            var convrt = Convert.ToInt32(txtSearch.Text);
            var del = (from d in dbcontext.DeliveryReceipts
                       where d.InvoiceNo == convrt
                       select d).ToList();

            LVDelivListInfo.Items.Clear();
            if (del != null)
            {

                foreach (var sqlDel in del)
                {
                    ListViewItem lvitems = LVDelivListInfo.Items.Add(sqlDel.DRid_PK.ToString());
                    //lvitems.SubItems.Add(sqlDel.Deliv_Addr.ToString());
                    lvitems.SubItems.Add(sqlDel.InvoiceNo.ToString());
                    lvitems.SubItems.Add(sqlDel.Receivedby.ToString());
                    lvitems.SubItems.Add(sqlDel.DateTransact.ToString());

                    var po = dbcontext.PurchOrder.Find(sqlDel.poid_FK);
                    lvitems.SubItems.Add(po.POnum.ToString());
                }

                if (txtSearch.Text == "")
                {
                    RefreshDB();
                }
            }
        }

      
    }
}
